package com.wanda.controller;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wanda.model.BuildModel;
import com.wanda.model.BuildProModel;
import com.wanda.model.JsonModelGeneral;
import com.wanda.model.ProjectModel;
import com.wanda.model.UnitModel;
import com.wanda.model.UnitPBModel;
import com.wanda.model.Wd_UserModel;
import com.wanda.service.BuildService;
import com.wanda.service.ProjectService;
import com.wanda.service.UnitService;
import com.wanda.util.DateFormat;

@Controller
@RequestMapping("qt/ht/unit") 
public class UnitController {

	@Autowired
	private UnitService unitService;
	@Autowired
	private ProjectService projectService;
	@Autowired
	private BuildService buildService;
	
	//添加单元信息
	@RequestMapping(value="/addUnit.do")
	public @ResponseBody Map<String,Object> add(String build_id,String project_id,
			String unit_code,int room_count,String delete_start,String create_time,
			HttpServletRequest request){
		
		System.out.println("进入添加单元controller");
		
		UnitModel model = new UnitModel();
		Wd_UserModel user = (Wd_UserModel)request.getSession().getAttribute("bean");
		
		model.setBuild_id(build_id);
		model.setProject_id(project_id);
		model.setUnit_code(unit_code);
		model.setRoom_count(room_count);
		model.setDelete_state("0");
		model.setCreate_time(DateFormat.getNowDate());
		model.setTel(user.getTel());

		unitService.insertUnit(model);
		
		Map<String,Object> map = new HashMap<String,Object>();
		
		System.out.println("添加成功");
		
		map.put("msg", "成功");
        
		return map;
	}
	
	@RequestMapping(value = "/selectAllByAdd.do")
	public ModelAndView showAddAll(HttpServletRequest request,String munber) {
		
		ModelAndView model = new ModelAndView();
		
		List<BuildProModel> listpp = null;
		List<BuildModel> listbu= new ArrayList<BuildModel>();
		List<ProjectModel> listp = null;
		List<ProjectModel> listpjm= new ArrayList<ProjectModel>();
		
		try {
	       listp = projectService.selectAll();
	       listpp = buildService.selectAll();
			Wd_UserModel user = (Wd_UserModel) request.getSession().getAttribute("bean");
		if (!(user.getTel().equals("admin")||user.getTel()==("admin"))) {
			for (ProjectModel p : listp) {
				if(p.getTel()!=null){
					if(p.getTel().equals(user.getTel())){
						listpjm.add(p);
						System.out.println("进入项目查询"+p.getTel());
						System.out.println("进入项目查询"+p.getProject_name());
					}	
				}
			}	
			for(BuildModel pp: listpp){
				if(pp.getTel()!=null){
					if(pp.getTel().equals(user.getTel())){
						listbu.add(pp);
						System.out.println("进入楼栋查询"+pp.getTel());
						System.out.println("进入楼栋查询"+pp.getBuild_name());
					}	
				}
		    }
		}
		if (!(user.getTel().equals("admin")||user.getTel()==("admin"))) {
		    System.err.println("wo bu shi admin1:" +listpjm.size());
		    System.err.println("wo bu shi admin1:" +listbu.size());
		    model.addObject("proj", listpjm);
		    model.addObject("bu",listbu);
		}else{
			System.err.println("wo  shi admin2:"+listp);
			System.err.println("wo  shi admin2:"+listpp);
			model.addObject("proj", listp);
			model.addObject("bu",listpp);
		}
		model.setViewName("/ht/addUnit.jsp");
		
		System.err.println("项目个数："+listp.size());
		System.err.println("楼栋个数："+listpp.size());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return model;

	}
	
	//查询所有单元信息
	@RequestMapping(value="/selectAll.do")
	public ModelAndView select(HttpServletRequest request){
		
		ModelAndView model = new ModelAndView();
		List<UnitPBModel> unit =null;	
		Wd_UserModel user = (Wd_UserModel) request.getSession().getAttribute("bean");
		try{
			
			if(user.getTel().equals("admin")){
				System.err.println("我是admin");
				unit=unitService.selectAll();
				System.err.println("listp:"+unit.toArray());
				model.setViewName("/ht/listUnit.jsp");
				model.addObject("units", unit);
				//request.getSession().setAttribute("units", unit);
			}else{
				System.err.println("我不是admin:"+user.getTel());
				unit = unitService.selectAlll(user.getTel());
				System.err.println("listppp:"+unit.size());
				model.setViewName("/ht/listUnit.jsp");
				model.addObject("units", unit);
				//request.getSession().setAttribute("units", unit);
			}
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return model;	
	}
	
	//查询所有单元信息(房间表需要用到单元名字)
	@RequestMapping(value="/selectAlls.do")
	public @ResponseBody Map<String,Object> showAlls(String id){
		
		Map<String,Object> map = new HashMap<String,Object>();

		List<UnitPBModel> model = null;
		model=unitService.selectAll();
		map.put("msg",model);
		
		return map;	
	}
	
	//修改单元信息
	@RequestMapping(value="/updateUnit.do")
	public String update(UnitModel model){
		
		System.out.println("进入修改单元信息controller");
		model.setUpdate_time(DateFormat.getNowDate());
		
		unitService.updateUnit(model);
		
		Map<String,Object> map = new HashMap<String,Object>();
		
		map.put("units", map);
		return "redirect:/qt/ht/unit/selectAll.do";		
	}
	
	//软删除单元信息
	@RequestMapping(value="/deleteUnit.do")
	public String delete(UnitModel model){
		
		System.out.println("进入删除单元controller");
		model.setDelete_time(DateFormat.getNowDate());

		model.setUnit_id(model.getUnit_id());
		
		int unit=unitService.deleteUnit(model);
		Map<String,Object> map = new HashMap<String,Object>();
		
		map.put("map", unit);
		
		return "redirect:/qt/ht/unit/selectAll.do";	
	}
	
	//查询某条单元记录
	@RequestMapping(value="/selectById.do")
	public ModelAndView selectById(String id){
		
		System.out.println("进入单条查询");
		
		ModelAndView model = new ModelAndView();
		UnitModel unit = null;	
		try{
			
			unit=unitService.selectById(id);	
			model.setViewName("/ht/updateUnit.jsp");
			model.addObject("unit",unit);
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return model;
	}
	
	@RequestMapping(value="/SelectBySql.do")
	public @ResponseBody JsonModelGeneral showBySql(String SelectBySql){
		System.err.println("SearchSql:"+SelectBySql);
		JsonModelGeneral json = new JsonModelGeneral();
		List<UnitModel> units = null;
		try{
			
			units=unitService.selectBySql(SelectBySql);
			json.setRows(units);
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return json;		
}
}
